#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<set>
#include<map>

using namespace std;

//int main()
//{
//	int n;
//	scanf("%d", &n);
//	while (n--)
//	{
//		int num;
//		scanf("%d", &num);
//		vector<int> a(num), b(num);
//		for (int i = 0; i < num; i++)
//			cin >> a[i];
//		for (int i = 0; i < num; i++)
//			cin >> b[i];
//		auto sum = [](vector<int>& v) {
//			int res = 0;
//			for (auto it : v)
//				res += it;
//			return res;
//		};
//		int ma = *min_element(a.begin(), a.end());
//		int sa = sum(a);
//		int mb = *min_element(b.begin(), b.end());
//		int sb = sum(b);
//
//		cout << min(ma * num + sb, mb * num + sa) << endl;
//	}
//	return 0;
//}


string smallestEquivalentString(string s1, string s2, string baseStr) {
    vector<int> us(26, -1);
    auto findroot = [&](int i) {
        while (us[i] != -1)
        {
            i = us[i];
        }
        return i;
    };

    for (int i = 0; i < s1.size(); i++)
    {
        int root1 = findroot(s1[i] - 'a'), root2 = findroot(s2[i] - 'a');
        if (root1 < root2)
            us[root2] = root1;
        else if (root1 > root2)
            us[root1] = root2;
    }

    for (int i = 0; i < baseStr.size(); i++)
    {
        baseStr[i] = findroot(baseStr[i] - 'a') + 'a';
    }
    return baseStr;
}

int main()
{
    string s1 = "leetcode", s2 = "programs", s3 = "sourcecode";
    smallestEquivalentString(s1, s2, s3);
    return 0;
}

int minOperations(vector<int>& nums, int k) {
    set<int> s;
    int count = 0;
    int i = nums.size() - 1;
    while (i >= 0)
    {
        if (s.size() == k)
            break;
        if (nums[i] >= 1 && nums[i] <= k)
            s.insert(nums[i]);
        i--;
        count++;
    }
    return count;
}

int minOperations(vector<int>& nums) {
    map<int, int> m;
    for (int i = 0; i < nums.size(); i++)
    {
        m[nums[i]]++;
    }

    int count = 0;
    for (auto it : m)
    {
        int n = it.second;
        if (n == 1)
            return -1;

        count += (n / 3);
        n %= 3;
        if (n == 1)
            count += 1;
        count += (n / 2);
    }
    return count;
}

//int main()
//{
//    /*vector<int> v = { 3,2,5,3,1 };
//    cout << minOperations(v, 3);*/
//
//    vector<int> v = { 2,3,3,2,2,4,2,3,4 };
//    cout << minOperations(v);
//    return 0;
//}